package com.mastercard.labs

import java.util.Date;
import java.util.List;
import org.codehaus.groovy.grails.validation.routines.UrlValidator

class Deal {

	String title
	String imageUrl
	String description
	String detailUrl
//	String location
	Country country
	Merchant merchant
	List categories
	double price
	Date startDate
	Date endDate
	String tags // List of single word string, separated by comma ,

	static constraints = {
		title nullable: false, blank: false, unique: true, maxSize: 100
		imageUrl nullable: true, maxSize: 255, validator: {UrlValidator urlValidator = new UrlValidator(); if (it == null || it == "" || urlValidator.isValid(it) || urlValidator.isValid("http://" + it)) return true; else return false;}
		description nullable: true, maxSize: 255
		detailUrl nullable: true, maxSize: 255
		country nullable: false, blank: false
		merchant nullable: true
		categories nullable: true
		price nullable: false, blank: false, maxSize: 10, validator: { val, obj -> val.toDouble() >= 0}
		//startDate nullable: false, validator: {it > new Date()-1}
		//endDate nullable: false, validator: { val, obj -> !val?.before(obj.startDate)}
		startDate nullable: false, validator: { val, obj -> log.info 'startdate:' +val +' with id:' + obj.id; if (val) { if (obj.id == null) {return val > new Date()-1} else { true}}} //Date oriDate =    Date.parse( "yyyy-MM-dd", params.dbStartDate ); log.info 'oriDate:' + oriDate; return !oriDate.after(val) } 
		endDate nullable: false, validator: { val, obj -> if (val && obj.startDate) !val?.before(obj.startDate)}
		tags  nullable: true, maxSize: 100
	}

	static hasMany=[categories:Category]

}
